package com.everflourish.yeah100.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.Window;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.everflourish.yeah100.R;

/**
 * 加载对话框
 *
 * @author peifen
 */
public class LoadDialog {

    public Dialog pd;

    private LoadDialog() {
    }

    public static LoadDialog show(Context context, CharSequence title, CharSequence message) {
        return show(context, title, message, false, null);
    }

    public static LoadDialog show(Context context, CharSequence title, CharSequence message, boolean cancelable) {
        return show(context, title, message, cancelable, null);
    }

    public static LoadDialog show(Context context, CharSequence title, CharSequence message, final RequestQueue queue) {
        return show(context, title, message, true, new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                queue.stop();
            }
        });
    }

    public static LoadDialog show(Context context, CharSequence title, CharSequence message, boolean backCancelable,
                                  OnCancelListener listener) {
        LoadDialog loadDialog = new LoadDialog();
        loadDialog.pd = new Dialog(context);
        loadDialog.pd.requestWindowFeature(Window.FEATURE_NO_TITLE);
        loadDialog.pd.setContentView(R.layout.dialog_load);
        TextView tv = (TextView) loadDialog.pd.findViewById(R.id.load_tv);
        tv.setText(message);
//        loadDialog.pd.setTitle(title);
        loadDialog.pd.getWindow().setBackgroundDrawableResource(R.color.transparent);
        loadDialog.pd.setCancelable(backCancelable);
        loadDialog.pd.setOnCancelListener(listener);
        loadDialog.pd.show();
        return loadDialog;
    }

    public static void dismiss(LoadDialog loadDialog) {
        if (loadDialog != null && loadDialog.pd != null) {
            loadDialog.pd.dismiss();
//            loadDialog.pd.cancel();
        }
    }
}